package com.eastfair.builder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.builder.dto.ReportDrawingPageQuery;
import com.eastfair.builder.dto.ReportDrawingSaveDTO;
import com.eastfair.builder.dto.ReportDrawingUpdateDTO;
import com.eastfair.builder.entity.ReportDrawing;
import com.eastfair.builder.service.BuilderReportDrawingAuditHistoryService;
import com.eastfair.builder.service.BuilderReportDrawingCategoryAuditHistoryService;
import com.eastfair.builder.service.ReportDrawingService;
import com.eastfair.builder.vo.ReportDrawingCategoryVO;
import com.eastfair.builder.vo.ReportDrawingVO;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.ReportDrawingCategoryAuditHistoryDTO;
import com.eastfair.home.vo.ReportDrawingAuditHistoryVO;
import com.eastfair.home.vo.ReportDrawingCategoryAuditHistoryVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 报馆图纸
 * </p>
 *
 * @author dq
 * @date 2022-08-06
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/reportDrawing")
@Api(value = "ReportDrawing", tags = "报馆图纸")
@PreAuth(replace = "builder:reportDrawing:")
public class ReportDrawingController extends SuperController<ReportDrawingService, Long, ReportDrawing, ReportDrawingPageQuery, ReportDrawingSaveDTO, ReportDrawingUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ReportDrawingService reportDrawingService;

    @Resource
    private BuilderReportDrawingCategoryAuditHistoryService builderReportDrawingCategoryAuditHistoryService;

    @Resource
    private BuilderReportDrawingAuditHistoryService builderReportDrawingAuditHistoryService;

    @Override
    public void handlerResult(IPage<ReportDrawing> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ReportDrawing> reportDrawingList = list
                .stream()
                .map((map) -> ReportDrawing.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(reportDrawingList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ReportDrawingPageQuery> params) {
        ReportDrawingPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 报馆ID查询图纸分类列表.
     *
     * @param reportSpaceId the report space id
     * @return the r
     */
    @GetMapping("/category/{reportSpaceId}")
    public R<List<ReportDrawingCategoryVO>> listCategoryByByReportSpaceId(@PathVariable("reportSpaceId") Long reportSpaceId) {
        log.info("listCategoryByByReportSpaceId - 查询图纸分类列表, reportSpaceId={}", reportSpaceId);
        return R.successDef(reportDrawingService.listCategoryVOByReportSpaceId(reportSpaceId));
    }

    /**
     * 报馆ID，图纸分类ID查询图纸列表.
     *
     * @param reportSpaceId the report space id
     * @param drawingId     the drawing id
     * @return the r
     */
    @GetMapping("/reportSpace")
    public R<List<ReportDrawingVO>> listByReportSpaceId(@RequestParam("reportSpaceId") Long reportSpaceId,
                                                        @RequestParam("drawingId") Long drawingId) {
        log.info("listByReportSpaceId - 报馆ID，图纸分类ID查询图纸列表, reportSpaceId={}, drawingId={}",
                reportSpaceId, drawingId);
        return R.successDef(reportDrawingService.listVOByReportSpaceId(reportSpaceId, drawingId));
    }

    /**
     * 分页查询报馆图纸分类审核历史列表.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/query/reportDrawingCategoryAuditHistory")
    public R<Page<ReportDrawingCategoryAuditHistoryVO>> queryReportDrawingCategoryAuditHistory(@RequestBody
                                                                                               PageParams<ReportDrawingCategoryAuditHistoryDTO> pageParams) {
        log.info("queryReportDrawingCategoryAuditHistory - 分页查询报馆图纸分类审核历史列表, pageParams={}", pageParams);
        Page<ReportDrawingCategoryAuditHistoryVO> page = builderReportDrawingCategoryAuditHistoryService.queryReportDrawingCategoryAuditHistory(pageParams);
        return R.successDef(page);
    }

    /**
     * 图纸分类审核历史ID查询图纸审核历史列表.
     *
     * @param reportDrawingCategoryAuditHistoryId the report drawing category audit history id
     * @return the r
     */
    @GetMapping("/reportDrawingCategoryAuditHistory/{reportDrawingCategoryAuditHistoryId}")
    public R<List<ReportDrawingAuditHistoryVO>> queryByReportDrawingCategoryAuditHistoryId(@PathVariable("reportDrawingCategoryAuditHistoryId")
                                                                                           Long reportDrawingCategoryAuditHistoryId) {
        log.info("queryByReportDrawingCategoryAuditHistoryId - 图纸分类审核历史ID查询图纸审核历史列表, reportDrawingCategoryAuditHistoryId={}", reportDrawingCategoryAuditHistoryId);
        List<ReportDrawingAuditHistoryVO> list
                = builderReportDrawingAuditHistoryService.queryByReportDrawingCategoryAuditHistoryId(reportDrawingCategoryAuditHistoryId);
        return R.successDef(list);
    }

    /**
     * 是否需要场馆审核
     *
     * @param reportSpaceId the report space id
     * @param isDouble      the is double
     * @param isPoint       the is point
     * @return the need venue audit
     */
    @GetMapping("/get/needVenueAudit")
    public R<Boolean> getNeedVenueAudit(@RequestParam("reportSpaceId") Long reportSpaceId,
                                        @RequestParam("isDouble") Integer isDouble,
                                        @RequestParam("isPoint") Integer isPoint) {
        log.info("getNeedVenueAudit - 是否需要场馆审核, reportSpaceId={}, isDouble={}, isPoint={}", reportSpaceId, isDouble, isDouble);
        try {
            return R.successDef(reportDrawingService.getNeedVenueAudit(reportSpaceId, isDouble, isPoint));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 图纸URL批量删除图纸记录.
     *
     * @param list the list
     * @return the r
     */
    @PostMapping("/remove/imageUrl")
    public R<Boolean> deleteByImageUrl(@RequestBody List<String> list) {
        log.info("deleteByImageUrl - 图纸URL批量删除图纸记录, list={}", list);
        return R.successDef(reportDrawingService.removeByImageUrl(list));
    }
}
